package com.yic.module.wms.controller.admin.producein;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.base.api.item.ItemApi;
import com.yic.module.base.api.item.dto.ItemRespDTO;
import com.yic.module.mes.api.mo.MoApi;
import com.yic.module.mes.api.mo.dto.MoRespDTO;
import com.yic.module.system.api.user.AdminUserApi;
import com.yic.module.system.api.user.dto.AdminUserRespDTO;
import com.yic.module.wms.dal.dataobject.warehouse.WarehouseDO;
import com.yic.module.wms.dal.dataobject.warehousearea.WarehouseAreaDO;
import com.yic.module.wms.dal.dataobject.warehouselocation.WarehouseLocationDO;
import com.yic.module.wms.service.warehouse.WarehouseService;
import com.yic.module.wms.service.warehousearea.WarehouseAreaService;
import com.yic.module.wms.service.warehouselocation.WarehouseLocationService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.wms.controller.admin.producein.vo.*;
import com.yic.module.wms.dal.dataobject.producein.ProduceInDO;
import com.yic.module.wms.convert.producein.ProduceInConvert;
import com.yic.module.wms.service.producein.ProduceInService;

@Tag(name = "管理后台 - 生产入库单")
@RestController
@RequestMapping("/wms/produce-in")
@Validated
public class ProduceInController {

    @Resource
    private ProduceInService produceInService;
    @Resource
    private FactoryApi factoryApi;
    @Resource
    private MoApi moApi;
    @Resource
    private ItemApi itemApi;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private WarehouseAreaService warehouseAreaService;
    @Resource
    private WarehouseLocationService warehouseLocationService;
    @Resource
    private AdminUserApi adminUserApi;


    @PostMapping("/create")
    @Operation(summary = "创建生产入库单")
    @PreAuthorize("@ss.hasPermission('wms:produce-in:create')")
    public CommonResult<Long> createProduceIn(@Valid @RequestBody ProduceInCreateReqVO createReqVO) {
        return success(produceInService.createProduceIn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产入库单")
    @PreAuthorize("@ss.hasPermission('wms:produce-in:update')")
    public CommonResult<Boolean> updateProduceIn(@Valid @RequestBody ProduceInUpdateReqVO updateReqVO) {
        produceInService.updateProduceIn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产入库单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:produce-in:delete')")
    public CommonResult<Boolean> deleteProduceIn(@RequestParam("id") Long id) {
        produceInService.deleteProduceIn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产入库单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:produce-in:query')")
    public CommonResult<ProduceInRespVO> getProduceIn(@RequestParam("id") Long id) {
        ProduceInDO produceIn = produceInService.getProduceIn(id);
        ProduceInRespVO resp = ProduceInConvert.INSTANCE.convert(produceIn);
        if (ObjectUtil.isNotEmpty(resp)){
            //封装工厂
            FactoryRespDTO factory = factoryApi.getFactory(resp.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                resp.setFactoryName(factory.getName());
            }
            //封装生产工单
            MoRespDTO mo = moApi.getMo(resp.getMoId());
            if (ObjectUtil.isNotEmpty(mo)) {
                resp.setMoCode(mo.getCode());
            }
            //封装产品
            ItemRespDTO item = itemApi.getItem(resp.getItemId());
            if (ObjectUtil.isNotEmpty(item)) {
                resp.setItemName(item.getName());
                resp.setItemCode(item.getCode());
                resp.setItemSpec(item.getSpec());
                resp.setDrawCode(item.getDrawCode());
            }
        }
        return success(resp);
    }

    @GetMapping("/list")
    @Operation(summary = "获得生产入库单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('wms:produce-in:query')")
    public CommonResult<List<ProduceInRespVO>> getProduceInList(@RequestParam("ids") Collection<Long> ids) {
        List<ProduceInDO> list = produceInService.getProduceInList(ids);
        return success(ProduceInConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产入库单分页")
    @PreAuthorize("@ss.hasPermission('wms:produce-in:query')")
    public CommonResult<PageResult<ProduceInRespVO>> getProduceInPage(@Valid ProduceInPageReqVO pageVO) {
        PageResult<ProduceInDO> pageResult = produceInService.getProduceInPage(pageVO);
        PageResult<ProduceInRespVO> respVo = ProduceInConvert.INSTANCE.convertPage(pageResult);
        respVo.getList().forEach(resp -> {
            //封装工厂
            FactoryRespDTO factory = factoryApi.getFactory(resp.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                resp.setFactoryName(factory.getName());
            }
            //封装生产工单
            MoRespDTO mo = moApi.getMo(resp.getMoId());
            if (ObjectUtil.isNotEmpty(mo)) {
                resp.setMoCode(mo.getCode());
            }
            //封装产品
            ItemRespDTO item = itemApi.getItem(resp.getItemId());
            if (ObjectUtil.isNotEmpty(item)) {
                resp.setItemName(item.getName());
                resp.setItemCode(item.getCode());
                resp.setItemSpec(item.getSpec());
                resp.setDrawCode(item.getDrawCode());
            }
            //封装仓库
            WarehouseDO warehouse = warehouseService.getWarehouse(resp.getWarehouseId());
            if (ObjectUtil.isNotEmpty(warehouse)) {
                resp.setWarehouseName(warehouse.getName());
            }
            //封装库区
            if (ObjectUtil.isNotNull(resp.getAreaId()) && !resp.getAreaId().equals(0L)) {
                WarehouseAreaDO warehouseArea = warehouseAreaService.getWarehouseArea(resp.getAreaId());
                if (ObjectUtil.isNotEmpty(warehouseArea)) {
                    resp.setAreaName(warehouseArea.getName());
                }
            }
            //封装库位
            if (ObjectUtil.isNotNull(resp.getLocationId()) && !resp.getLocationId().equals(0L)) {
                WarehouseLocationDO warehouseLocation = warehouseLocationService.getWarehouseLocation(resp.getLocationId());
                if (ObjectUtil.isNotEmpty(warehouseLocation)) {
                    resp.setLocationName(warehouseLocation.getName());
                }
            }
            //封装制单人
            AdminUserRespDTO user = adminUserApi.getUser(resp.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                resp.setUserName(user.getNickname());
            }
        });
        return success(respVo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产入库单 Excel")
    @PreAuthorize("@ss.hasPermission('wms:produce-in:export')")
    @OperateLog(type = EXPORT)
    public void exportProduceInExcel(@Valid ProduceInExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProduceInDO> list = produceInService.getProduceInList(exportReqVO);
        // 导出 Excel
        List<ProduceInExcelVO> datas = ProduceInConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "生产入库单.xls", "数据", ProduceInExcelVO.class, datas);
    }

    @PutMapping("/approve")
    @Operation(summary = "审核接口")
    @PreAuthorize("@ss.hasPermission('wms:produce-in:approve')")
    public CommonResult<Boolean> approve(@RequestParam("id") Long id){
        produceInService.approve(id);
        return success(true);
    }

    @PutMapping("/cancel")
    @Operation(summary = "撤审接口")
    @PreAuthorize("@ss.hasPermission('wms:produce-in:approve')")
    public CommonResult<Boolean> cancel(@RequestParam("id") Long id){
        produceInService.cancel(id);
        return success(true);
    }

}
